package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.*;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品管理 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/sys/storeCommodity")
@Api(description = "商品管理")
public class StoreCommodityController extends BaseController<IZslStoreCommodityService, ZslStoreCommodity> {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IZslOriginService iZslOriginService;
    @Autowired
    private IZslBrowseHistoryService iZslBrowseHistoryService;
    @Autowired
    private IZslFollowService iZslFollowService;
    @Autowired
    private IQuotationService iQuotationService;
    @Autowired
    private IZslAuthenticationService iZslAuthenticationService;
    @Autowired
    private CompanyAphorismService companyAphorismService;
    @Autowired
    private OnlineIMService onlineIMService;
    @Autowired
    private IZslStoreService iZslStoreService;


    @ApiOperation("设置产品权重")
    @RequestMapping(value = "/setInfoWeight", method = RequestMethod.POST)
    public ReturnObject setInfoWeight(@ApiParam("商品id") @RequestParam Long id,
                                      @ApiParam("权重（0：普通商品，1：优势产品，2：橱窗产品）") @RequestParam Integer weight) {
        if (iSysUserService.selectById(BaseContextHandler.getUserID()).getType() != 2) {
            return new ReturnObject<>(1, "只有加盟平台才可使用该功能");
        }
        if (weight == 1) {
            if (baseService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq("user_id",
                    BaseContextHandler.getUserID()).eq("weight", weight)) >= 20) {
                return new ReturnObject<>(1, "每个店铺只能设置20个优势产品");
            }

        }
        if (weight == 2) {
            if (baseService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq("user_id",
                    BaseContextHandler.getUserID()).eq("weight", weight)) >= 10) {
                return new ReturnObject<>(1, "每个店铺只能设置10个橱窗产品");
            }

        }
        ZslStoreCommodity zslStoreCommodity = baseService.selectById(id);
        zslStoreCommodity.setWeight(weight);
        baseService.updateById(zslStoreCommodity);
        return new ReturnObject<>(0, "设置成功");
    }

    @IgnoreAuth
    @ApiOperation("获取广告")
    @RequestMapping(value = "/getAdvertisement", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getAdvertisement(@ApiParam("条数") @RequestParam(defaultValue = "10") int limit) {
        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.orderBy("info.id", false);
        ew.eq("info.type", 0);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(1, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取我发布的求购信息分页")
    @RequestMapping(value = "/getMyBuyPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getMyBuyPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                              @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                              @ApiParam("商品类型（传中文）") String type,
                                                              @ApiParam("通用查询条件") String name,
                                                              @ApiParam("商品状态（0：上架，1：下架）") Integer state,
                                                              @ApiParam("商品起始价钱") BigDecimal startPrice,
                                                              @ApiParam("商品结束价钱") BigDecimal endPrice,
                                                              @ApiParam("自定义类型") String customType) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(customType)) {
            ew.like("info.custom_type", customType);
        }
        if (state != null) {
            ew.eq("info.state", state);
        }
        if (startPrice != null) {
            ew.ge("info.price", startPrice);
        }
        if (endPrice != null) {
            ew.le("info.price", endPrice);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 1);
        ew.eq("info.user_id", BaseContextHandler.getUserID());
        ew.orderBy("info.crt_time", false);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslStoreCommodity info : obj.getRecords()) {
            info.setQuotation(0);
            if (iQuotationService.selectCount(new EntityWrapper<Quotation>().eq("commodity_id", info.getId())) > 0) {
                info.setQuotation(1);
            }

        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取我的商品分页")
    @RequestMapping(value = "/getMyPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getMyPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                           @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                           @ApiParam("商品类型（传中文）") String type,
                                                           @ApiParam("通用查询条件") String name,
                                                           @ApiParam("商品状态（0：上架，1：下架）") Integer state,
                                                           @ApiParam("授权品牌Id") Long originId,
                                                           @ApiParam("商品起始价钱") BigDecimal startPrice,
                                                           @ApiParam("商品结束价钱") BigDecimal endPrice,
                                                           @ApiParam("权重（0：普通商品，1：优势产品，2：橱窗产品）") Integer weight,
                                                           @ApiParam("自定义类型") String customType) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        if (weight != null) {
            ew.eq("info.weight", weight);
        }
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(customType)) {
            ew.like("info.custom_type", customType);
        }
        if (state != null) {
            ew.eq("info.state", state);
        }
        if (originId != null) {
            ew.eq("info.origin_id", originId);
        }
        if (startPrice != null) {
            ew.ge("info.price", startPrice);
        }
        if (endPrice != null) {
            ew.le("info.price", endPrice);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("status", 0);
        ew.eq("info.type", 0);
        ew.eq("info.user_id", BaseContextHandler.getUserID());
        ew.orderBy("info.crt_time", false);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @IgnoreAuth
    @ApiOperation("获取商品分页")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                         @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                         @ApiParam("商品类型（传中文）") String type,
                                                         @ApiParam("通用查询条件") String name,
                                                         @ApiParam("店铺id") Long storeId,
                                                         @ApiParam("品牌id") Long[] originId,
                                                         @ApiParam("商品起始价钱") BigDecimal startPrice,
                                                         @ApiParam("商品结束价钱") BigDecimal endPrice,
                                                         @ApiParam("权重（0：普通商品，1：优势产品，2：橱窗产品）") Integer weight,
                                                         @ApiParam("排序(crt_time:默认，price:商品价格，grade：评分，volume：成交量)") @RequestParam(defaultValue = "crt_time") String sort,
                                                         @ApiParam("排序方式(是否正序)") @RequestParam(defaultValue = "false") Boolean isAsc) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.state", 0);
        ew.eq("info.status", 0);
        if (weight != null) {
            ew.eq("info.weight", weight);
        }
        if (startPrice != null) {
            ew.ge("info.price", startPrice);
        }
        if (originId != null && originId.length > 0) {
            ew.in("info.origin_id", Arrays.asList(originId));
        }
        if (endPrice != null) {
            ew.le("info.price", endPrice);
        }
        if (storeId != null) {
            ew.eq("info.store_id", storeId);
        }
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 0);
        ew.orderBy("info." + sort, isAsc);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslStoreCommodity zslStoreCommodity : obj.getRecords()) {
            CompanyAphorism companyAphorism =
                    companyAphorismService.selectOne(new EntityWrapper<CompanyAphorism>().eq("store_id", zslStoreCommodity.getStoreId()));
            if (companyAphorism != null) {

                zslStoreCommodity.setAphorismContent(companyAphorism.getAphorismContent());
            }
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("新增商品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> add(
            @ApiParam("商品类型（存中文多个类型用逗号隔开）") @RequestParam String commodityType,
            @ApiParam("类型（0：供应，1：求购）") @RequestParam Integer type,
            @ApiParam("是否发布（0：保存到草稿，1：发布到待上架列表）") @RequestParam Integer isPublish,
            @ApiParam("信息标题") @RequestParam String name,
            @ApiParam("配件图号") @RequestParam String drawingNo,
            @ApiParam("品牌id（授权的品牌）") @RequestParam Long originId,
            @ApiParam("产地") @RequestParam String productionPlace,
            @ApiParam("发货地") @RequestParam String dispatchPlace,
            @ApiParam("发货时间 0:与卖家协商 1:立即发货  2:24小时内发货") @RequestParam Integer dispatchTime,
            @ApiParam("产品性质（0：全新国产，1：原装进口，2：拆车件/再制造）") @RequestParam Integer property,
            @ApiParam("商品图片url（多张图片逗号隔开）") @RequestParam String picUrl,
            @ApiParam("内容") @RequestParam String content,
            @ApiParam("计量单位") @RequestParam String prickle,
            @ApiParam("最小起订数") @RequestParam Integer minimumOrder,
            @ApiParam("单价") @RequestParam BigDecimal price,
            @ApiParam("单价类型 0、参考价格/浮动价格 1、实价/一口价") @RequestParam Integer priceType,
            @ApiParam("运费信息 0:卖家承担运费 1:买家承担运费") @RequestParam Integer freightCharge,
            @ApiParam("自定义类型（多个逗号隔开）") @RequestParam String customType,
            @ApiParam("型号") @RequestParam String origin
    ) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        if (sysUser.getStoreId() == null) {
            return new ReturnObject<>(1, "当前账号未绑定店铺信息，请先填写基本信息", null);
        }

        ZslStoreCommodity info = new ZslStoreCommodity();
        info.setStoreId(sysUser.getStoreId());
        info.setStoreName(sysUser.getStoreName());
        info.setUserId(BaseContextHandler.getUserID());
        info.setName(name);
        info.setDrawingNo(drawingNo);
        info.setDispatchPlace(dispatchPlace);
        info.setDispatchTime(dispatchTime);
        info.setMinimumOrder(minimumOrder);
        info.setFreightCharge(freightCharge);
        info.setState(1);
        if (isPublish == 0) {
            info.setStatus(1);
        } else {
            info.setStatus(0);
        }
        info.setPrice(price);
        info.setPriceType(priceType);
        info.setOrigin(origin);
        info.setOriginId(originId);
        info.setCommodityType(commodityType);
        if (originId != null) {
            info.setOriginName(iZslOriginService.selectById(originId).getName());
        }
        info.setPicUrl(picUrl);
        info.setVolume(0);
        info.setProductionPlace(productionPlace);
        info.setPrickle(prickle);
        info.setProperty(property);
        info.setCustomType(customType);
        info.setContent(content);
        info.setType(type);
        info.setWeight(0);
        baseService.insert(info);
        return new ReturnObject<>(0, "获取成功", info);
    }

    @ApiOperation("编辑商品")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> edit(@ApiParam("id") @RequestParam Long id,
                                                @ApiParam("商品信息") @RequestParam String name,
                                                @ApiParam("品牌id（授权的品牌）") @RequestParam Long originId,
                                                @ApiParam("产地") @RequestParam String productionPlace,
                                                @ApiParam("产品性质（0：全新，1：拆车件，2：再制造）") @RequestParam Integer property,
                                                @ApiParam("单价") @RequestParam BigDecimal price,
                                                @ApiParam("单价类型 0、参考价格/浮动价格 1、实价/一口价") @RequestParam Integer priceType,
                                                @ApiParam("计量单位") @RequestParam String prickle,
                                                @ApiParam("自定义类型（多个逗号隔开）") @RequestParam String customType,
                                                @ApiParam("型号") @RequestParam String origin,
                                                @ApiParam("商品类型（存中文多个类型用逗号隔开）") @RequestParam String commodityType,
                                                @ApiParam("商品图片url（多张图片逗号隔开）") @RequestParam String picUrl,
                                                @ApiParam("内容") @RequestParam String content,
                                                @ApiParam("类型（0：供应，1：求购）") @RequestParam Integer type,
                                                @ApiParam("是否发布（0：保存到草稿，1：发布到待上架列表）") @RequestParam Integer isPublish
    ) {
        ZslStoreCommodity info = baseService.selectById(id);
        info.setUserId(BaseContextHandler.getUserID());
        info.setName(name);
        info.setPrice(price);
        info.setPriceType(priceType);
        info.setOrigin(origin);
        info.setOriginId(originId);
        info.setCommodityType(commodityType);
        if (originId != null) {
            info.setOriginName(iZslOriginService.selectById(originId).getName());
        }
        info.setPicUrl(picUrl);
        info.setVolume(0);
        info.setProductionPlace(productionPlace);
        info.setPrickle(prickle);
        info.setProperty(property);
        info.setCustomType(customType);
        info.setContent(content);
        info.setType(type);
        if (isPublish == 1) {
            info.setStatus(0);
        } else {
            info.setStatus(1);
        }
        baseService.updateById(info);
        return new ReturnObject<>(0, "更新成功");
    }

    @IgnoreAuth
    @ApiOperation("查看商品详情")
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> view(@ApiParam("id") @RequestParam Long id, @ApiParam("是否登录 0、登录 传空表示未登录") @RequestParam Integer type) {
        ZslStoreCommodity info = baseService.selectById(id);
        if (info != null) {
            info.setFocus(0);
            if (type != null) {
                if (iZslFollowService.selectCount(new EntityWrapper<ZslFollow>().eq("user_id",
                        BaseContextHandler.getUserID()).eq("commodty_id", id)) > 0) {
                    info.setFocus(1);
                }
                //增加查看记录
                if (iZslBrowseHistoryService.selectCount(new EntityWrapper<ZslBrowseHistory>().eq("user_id",
                        BaseContextHandler.getUserID()).eq("commodity_id", id)) == 0) {
                    iZslBrowseHistoryService.insert(new ZslBrowseHistory(BaseContextHandler.getUserID(), info.getStoreId(),
                            id, null, null, null));
                }
            }
        }
        return new ReturnObject<>(0, "获取成功", info);
    }

    @ApiOperation("批量下架")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> del(@ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setState(1);
            baseService.updateById(info);


        }
        return new ReturnObject<>(0, "下架成功", null);
    }

    @ApiOperation("批量上架")
    @RequestMapping(value = "/grounding", method = RequestMethod.POST)
    public ReturnObject<List<Long>> grounding(@ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setState(0);
            baseService.updateById(info);

        }
        return new ReturnObject<>(0, "上架成功");


    }

    @ApiOperation("审核通过/或者拒绝")
    @RequestMapping(value = "/agree", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> agree(@ApiParam("id") @RequestParam Long id,
                                                 @ApiParam("isAgree") @RequestParam Integer isAgree) {
        ZslStoreCommodity zslStoreCommodity = baseService.selectById(id);
        zslStoreCommodity.setIsAgree(isAgree);
        baseService.updateById(zslStoreCommodity);
        return new ReturnObject<>(0, "审核成功", null);

    }

    @ApiOperation("批量放草稿箱")
    @RequestMapping(value = "/drafts", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> drafts(@ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setStatus(1);
            info.setState(1);
            baseService.updateById(info);

        }
        return new ReturnObject<>(0, "删除成功", null);

    }

    @ApiOperation("批量移除草稿箱")
    @RequestMapping(value = "/moveDrafts", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> moveDrafts(@ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setStatus(0);
            baseService.updateById(info);

        }
        return new ReturnObject<>(0, "移除成功", null);

    }

    @ApiOperation("批量彻底删除")
    @RequestMapping(value = "/completeDel", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> completeDel(@ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setStatus(2);
            baseService.updateById(info);
        }
        return new ReturnObject<>(0, "删除成功", null);

    }


    @ApiOperation("批量设置分类")
    @RequestMapping(value = "/setCommodityType", method = RequestMethod.POST)
    public ReturnObject setCommodityType(@ApiParam("商品类型（存中文多个类型用逗号隔开）") @RequestParam String commodityType,
                                         @ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setCommodityType(commodityType);
            baseService.updateById(info);

        }
        return new ReturnObject<>(0, "设置成功");
    }

    @ApiOperation("批量设置自定义分类")
    @RequestMapping(value = "/setCustomType", method = RequestMethod.POST)
    public ReturnObject setCustomType(@ApiParam("自定义分类（存中文多个类型用逗号隔开）") @RequestParam String customType,
                                      @ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslStoreCommodity> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslStoreCommodity info : list) {
            info.setCustomType(customType);
            baseService.updateById(info);

        }


        return new ReturnObject<>(0, "设置成功");
    }


    @ApiOperation("查看认证详情")
    @RequestMapping(value = "/findAuthenticationInfoById", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> findAuthenticationInfoById(@ApiParam("id") @RequestParam Long id) {
        ZslStoreCommodity info = baseService.selectById(id);
        return new ReturnObject<>(0, "获取成功", info);
    }


    @ApiOperation("获取全部待审核产品分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getAuditPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getAuditPage(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("配件图号/信息标题") @RequestParam String name) {
        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("is_agree", 1);
        ew.eq("status", 0);
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.drawing_no", name).or().like("info.name", name);
        }
        ew.orderBy("crt_time", false);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取我店铺中精品/橱窗商品")
    @RequestMapping(value = "/getBoutique", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getBoutique(@ApiParam("权重（0：普通商品，1：精品产品，2：橱窗产品）") Integer weight,
                                                             @ApiParam("店铺id") Long storeId
    ) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("weight", weight);
        ew.eq("state", 0);
        ew.eq("status", 0);
        ew.eq("store_id", storeId);
        ew.orderBy("crt_time", false);

        List<ZslStoreCommodity> list = baseService.selectList(ew);
        return new ReturnObject<>(0, "获取成功", list);
    }

    @ApiOperation("获取产品中所有的产地列表")
    @RequestMapping(value = "/getAllAera", method = RequestMethod.POST)
    @IgnoreAuth
    public ReturnObject<List<String>> getAllAera(@ApiParam("店铺id") Long storeId) {
        List<String> list = new ArrayList<>();
        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        if (storeId != null) {
            ew.eq("store_id", storeId);
        }
        ew.eq("state", 0);
        ew.eq("status", 0);
        ew.orderBy("crt_time", false);
        List<ZslStoreCommodity> zslStoreCommodityList = baseService.selectList(ew);
        if (zslStoreCommodityList != null && zslStoreCommodityList.size() > 0) {
            for (int i = 0; i < zslStoreCommodityList.size(); i++) {
                list.add(zslStoreCommodityList.get(i).getProductionPlace());
            }
        }
        return new ReturnObject<>(0, "获取成功", list);
    }


    @ApiOperation("获取销售中商品分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getSalePage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getSalePage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                             @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                             @ApiParam("商品类型（传中文）") @RequestParam String type,
                                                             @ApiParam("通用查询条件") @RequestParam String name,
                                                             @ApiParam("店铺id") @RequestParam Long storeId,
                                                             @ApiParam("品牌id") @RequestParam Long[] originId,
                                                             @ApiParam("商品起始价钱") @RequestParam BigDecimal startPrice,
                                                             @ApiParam("商品结束价钱") @RequestParam BigDecimal endPrice,
                                                             @ApiParam("权重（0：普通商品，1：优势产品，2：橱窗产品）") @RequestParam Integer weight,
                                                             @ApiParam("排序(crt_time:默认，price:商品价格)") @RequestParam(defaultValue = "crt_time") String sort,
                                                             @ApiParam("排序方式(是否正序)") @RequestParam(defaultValue = "false") Boolean isAsc) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.state", 0);
        ew.eq("info.status", 0);
        if (weight != null) {
            ew.eq("info.weight", weight);
        }
        if (startPrice != null) {
            ew.ge("info.price", startPrice);
        }
        if (originId != null && originId.length > 0) {
            ew.in("info.origin_id", Arrays.asList(originId));
        }
        if (endPrice != null) {
            ew.le("info.price", endPrice);
        }
        if (storeId != null) {
            ew.eq("info.store_id", storeId);
        }
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 0);
        ew.orderBy("info." + sort, isAsc);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslStoreCommodity zslStoreCommodity : obj.getRecords()) {
            CompanyAphorism companyAphorism =
                    companyAphorismService.selectOne(new EntityWrapper<CompanyAphorism>().eq("store_id", zslStoreCommodity.getStoreId()));
            if (companyAphorism != null) {
                zslStoreCommodity.setAphorismContent(companyAphorism.getAphorismContent());
            }
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取未上架商品分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getWaitSalePage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getWaitSalePage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                 @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                                 @ApiParam("商品类型（传中文）") @RequestParam String type,
                                                                 @ApiParam("通用查询条件") @RequestParam String name,
                                                                 @ApiParam("店铺id") @RequestParam Long storeId,
                                                                 @ApiParam("品牌id") @RequestParam Long[] originId,
                                                                 @ApiParam("商品起始价钱") @RequestParam BigDecimal startPrice,
                                                                 @ApiParam("商品结束价钱") @RequestParam BigDecimal endPrice,
                                                                 @ApiParam("权重（0：普通商品，1：优势产品，2：橱窗产品）") @RequestParam Integer weight,
//                                                                 @ApiParam("审核状态：0、审核通过 1、待审核；2、未通过）") @RequestParam Integer isAgree,
                                                                 @ApiParam("排序(crt_time:默认，price:商品价格)") @RequestParam(defaultValue = "crt_time") String sort,
                                                                 @ApiParam("排序方式(是否正序)") @RequestParam(defaultValue = "false") Boolean isAsc) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.state", 1);
        ew.eq("info.status", 0);
        if (weight != null) {
            ew.eq("info.weight", weight);
        }
        if (startPrice != null) {
            ew.ge("info.price", startPrice);
        }
        if (originId != null && originId.length > 0) {
            ew.in("info.origin_id", Arrays.asList(originId));
        }
        if (endPrice != null) {
            ew.le("info.price", endPrice);
        }
        if (storeId != null) {
            ew.eq("info.store_id", storeId);
        }
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 0);
        ew.orderBy("info." + sort, isAsc);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslStoreCommodity zslStoreCommodity : obj.getRecords()) {
            CompanyAphorism companyAphorism =
                    companyAphorismService.selectOne(new EntityWrapper<CompanyAphorism>().eq("store_id", zslStoreCommodity.getStoreId()));
            if (companyAphorism != null) {
                zslStoreCommodity.setAphorismContent(companyAphorism.getAphorismContent());
            }
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取全部草稿箱分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getDraftPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getDraftPage(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("status", 1);
        ew.eq("store_id", sysUser.getStoreId());
        ew.orderBy("crt_time", false);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取供应商品分页")
    @IgnoreAuth
    @RequestMapping(value = "/getClassifyPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getClassifyPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                 @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                                 @ApiParam("是否分类：0、分类，1、未分类") @RequestParam String classify,
                                                                 @ApiParam("通用查询条件") @RequestParam String name,
                                                                 @ApiParam("排序(crt_time:默认，price:商品价格)") @RequestParam(defaultValue = "crt_time") String sort,
                                                                 @ApiParam("排序方式(是否正序)") @RequestParam(defaultValue = "false") Boolean isAsc) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.state", 0);
        ew.eq("info.status", 0);

        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        ew.eq("info.store_id", sysUser.getStoreId());

        if (classify.equals("1")) {
            ew.isNull("info.commodity_type");
        } else {
            ew.isNotNull("info.commodity_type");
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 0);
        ew.orderBy("info." + sort, isAsc);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        for (ZslStoreCommodity zslStoreCommodity : obj.getRecords()) {
            CompanyAphorism companyAphorism =
                    companyAphorismService.selectOne(new EntityWrapper<CompanyAphorism>().eq("store_id", zslStoreCommodity.getStoreId()));
            if (companyAphorism != null) {

                zslStoreCommodity.setAphorismContent(companyAphorism.getAphorismContent());
            }
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取前端产品分页")
    @IgnoreAuth
    @RequestMapping(value = "/getBeforeProductsPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getBeforeProductsPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                       @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                                       @ApiParam("商品类型（传中文）") @RequestParam(required = false) String type,
                                                                       @ApiParam("通用查询条件") @RequestParam String name,
                                                                       @ApiParam("店铺id") @RequestParam Long storeId,
                                                                       @ApiParam("品牌id") @RequestParam Long originId,
                                                                       @ApiParam("地区id") @RequestParam Long districtId,
                                                                       @ApiParam("商品起始价钱") BigDecimal startPrice,
                                                                       @ApiParam("商品结束价钱") BigDecimal endPrice,
                                                                       @ApiParam("经营模式 0、生产厂家 1、经销批发 2、代理 3、其他") @RequestParam Integer businessModel,
                                                                       @ApiParam("发货时间 0、当天 1、三天内  2、一周内 3、与卖家协商") @RequestParam Integer dispatchTime,
                                                                       @ApiParam("产地") @RequestParam String productionPlace,
                                                                       @ApiParam("产品性质（0：全新国产，1：原装进口，2：拆车件/再制造）") @RequestParam Integer property,
                                                                       @ApiParam("是否认证商家（0、是，1、不是）") @RequestParam() Integer isAuthentication,
                                                                       @ApiParam("排序(crt_time:默认，price:商品价格)") @RequestParam(defaultValue = "crt_time") String sort,
                                                                       @ApiParam("排序方式(是否正序)") @RequestParam(defaultValue = "false") Boolean isAsc) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.state", 0);
        ew.eq("info.status", 0);
        if (storeId != null) {
            ew.eq("info.store_id", storeId);
        }
        if (originId != null) {
            ew.eq("info.origin_id", originId);
        }
        if (districtId != null) {
            ew.eq("info.district_id", districtId);
        }
        if (dispatchTime != null) {
            ew.eq("info.dispatch_time", dispatchTime);
        }

        if (startPrice != null) {
            ew.ge("info.price", startPrice);
        }

        if (endPrice != null) {
            ew.le("info.price", endPrice);
        }

        if (property != null) {
            ew.eq("info.property", property);
        }
        List<Long> storeIds = new ArrayList<>();

        //获取认证商家
        List<ZslAuthentication> zslasByAuth = new ArrayList<>();
        if (businessModel != null) {
            if (isAuthentication != null) {
                if (isAuthentication == 0) {
                    zslasByAuth = iZslAuthenticationService.selectList(new EntityWrapper<ZslAuthentication>().eq("state", 2).eq("business_model", businessModel));
                } else {
                    zslasByAuth = iZslAuthenticationService.selectList(new EntityWrapper<ZslAuthentication>().eq("business_model", businessModel));
                }
            } else {
                zslasByAuth = iZslAuthenticationService.selectList(new EntityWrapper<ZslAuthentication>().eq("business_model", businessModel));
            }
        } else {
            if (isAuthentication != null) {

                if (isAuthentication == 0) {
                    zslasByAuth = iZslAuthenticationService.selectList(new EntityWrapper<ZslAuthentication>().eq("state", 2));
                } else {
                    zslasByAuth = iZslAuthenticationService.selectList(new EntityWrapper<ZslAuthentication>());
                }
            } else {
                zslasByAuth = iZslAuthenticationService.selectList(new EntityWrapper<ZslAuthentication>());
            }
        }
        if (zslasByAuth != null && zslasByAuth.size() > 0) {

            for (ZslAuthentication authentication : zslasByAuth) {
                if (authentication.getStoreId() != null) {
                    storeIds.add(authentication.getStoreId());
                }
            }

            if (storeIds != null && storeIds.size() > 0) {

                ew.in("info.store_id", storeIds);
            }
        }
        if (StringUtils.isNotBlank(type)) {
            ew.eq("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(productionPlace)) {
            ew.like("info.production_place", productionPlace);
        }

        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 0);
        ew.orderBy("info." + sort, isAsc);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        if (obj.getRecords() != null && obj.getRecords().size() > 0) {
            for (ZslStoreCommodity zslStoreCommodity : obj.getRecords()) {
                ZslAuthentication zslAuthentication = iZslAuthenticationService.selectOne(new EntityWrapper<ZslAuthentication>().eq("store_id", zslStoreCommodity.getStoreId()));
                zslStoreCommodity.setZslAuthentication(zslAuthentication);
                {
                    CompanyAphorism companyAphorism =
                            companyAphorismService.selectOne(new EntityWrapper<CompanyAphorism>().eq("store_id", zslStoreCommodity.getStoreId()));
                    if (companyAphorism != null) {

                        zslStoreCommodity.setAphorismContent(companyAphorism.getAphorismContent());
                    }
                }
            }

        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取我店铺最新产品")
    @IgnoreAuth
    @RequestMapping(value = "/getNewProducts", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getNewProducts(@ApiParam("店铺id") Long storeId
    ) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("status", 0);
        ew.eq("store_id", storeId);
        ew.orderBy("crt_time", false);

        List<ZslStoreCommodity> list = baseService.selectList(ew);
        return new ReturnObject<>(0, "获取成功", list);
    }


    @ApiOperation("下架并回复意见")
    @RequestMapping(value = "/undercarriage", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodity> undercarriage(@ApiParam("id") @RequestParam Long id, @ApiParam("回复内容") @RequestParam String content,
                                                  @ApiParam("flag :0、下架 1、禁用") @RequestParam Integer flag) {
        ZslStoreCommodity zslStoreCommodity = baseService.selectById(id);
        zslStoreCommodity.setState(1);
        if(flag==1){
            zslStoreCommodity.setStatus(1);
        }
        baseService.updateById(zslStoreCommodity);

        SysUser sysUser = iSysUserService.selectById(zslStoreCommodity.getUserId());
        SysUser managerUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OnlineIM info = new OnlineIM();
        info.setType(1);
        info.setName(managerUser.getUsername());
        info.setHeadPic(managerUser.getHeadPortraits());
        info.setPhone(managerUser.getPhone());
        info.setFromUserId(managerUser.getId());
        if(flag==0){
            info.setContent("商品下架提醒：您的商品【" + zslStoreCommodity.getName() + "】已经被强制下架。下架理由为：" + content);
        }else{
            info.setContent("商品禁用提醒：您的商品【" + zslStoreCommodity.getName() +
                    "】已经被强制禁用。禁用理由为：" + content);
        }
        info.setUserId(sysUser.getId());
        info.setStoreId(sysUser.getStoreId());
        info.setIsDeal(1);
        ZslStore zslStore = iZslStoreService.selectById(sysUser.getStoreId());
        info.setStoreName(zslStore.getName());
        onlineIMService.insert(info);

        return new ReturnObject<>(0, "下架成功");
    }

    @ApiOperation("获取上架产品分页")
    @IgnoreAuth
    @RequestMapping(value = "/getUpperPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getUpperPage(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("配件图号/信息标题") @RequestParam String name,
            @ApiParam("上架状态 0、上架中 1、已下架") @RequestParam Long saleType) {
        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.status", 0);
        if (saleType != null) {
            ew.eq("info.state", saleType);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.drawing_no", name).or().like("info.name",
                    name).or().like("info.origin_name", name).or().like("info" +
                    ".store_name", name);
        }
        ew.orderBy("info.crt_time", false);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("管理后台通过店铺id获取上架产品分页")
    @RequestMapping(value = "/getUpperPageByStoreId", method =
            RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getUpperPageByStoreId(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("店铺id") @RequestParam Long storeId) {
        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        ew.eq("info.state", 0);
        ew.eq("info.status", 0);
        ew.eq("info.store_id", storeId);
        ew.orderBy("info.crt_time", false);
        Page<ZslStoreCommodity> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

}

